package com.iknow.file;

import android.content.Context;
import com.iknow.util.IKFileUtil;
import com.iknow.util.Loger;
import com.iknow.util.StringUtil;
import com.iknow.util.SystemUtil;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

public class CacheSystem
{
  private static final String apkPath;
  private static final String audioPath;
  private static final String cachePath = "cache" + File.separator;
  private static final String imagePath;
  private static final String tag = "com.iknow.file.CacheSystem";
  private static final String thumbmailPath;
  private final String TAG;
  public Context ctx = null;
  private MessageDigest mDigest;

  static
  {
    apkPath = "apk" + File.separator;
    imagePath = "images" + File.separator;
    audioPath = "audio" + File.separator;
    thumbmailPath = imagePath + "thumbmail" + File.separator;
  }

  public CacheSystem(Context paramContext)
  {
    this.TAG = "CacheSystem";
    this.mDigest = null;
    this.ctx = paramContext;
  }

  private String getHashString(MessageDigest paramMessageDigest)
  {
    StringBuilder localStringBuilder = new StringBuilder();
    byte[] arrayOfByte = paramMessageDigest.digest();
    int i = arrayOfByte.length;
    int j = 0;
    while (true)
    {
      if (j >= i)
        return localStringBuilder.toString();
      int k = arrayOfByte[j];
      localStringBuilder.append(Integer.toHexString(0xF & k >> 4));
      localStringBuilder.append(Integer.toHexString(k & 0xF));
      ++j;
    }
  }

  private String getMd5(String paramString)
  {
    getMessageDigest().update(paramString.getBytes());
    return getHashString(getMessageDigest());
  }

  // ERROR //
  private int write(InputStream paramInputStream, File paramFile)
  {
    // Byte code:
    //   0: aload_2
    //   1: ifnull +17 -> 18
    //   4: aload_2
    //   5: invokevirtual 114	java/io/File:exists	()Z
    //   8: ifeq +10 -> 18
    //   11: aload_2
    //   12: invokevirtual 117	java/io/File:canWrite	()Z
    //   15: ifne +8 -> 23
    //   18: bipush 255
    //   20: istore_3
    //   21: iload_3
    //   22: ireturn
    //   23: aload_1
    //   24: ifnonnull +9 -> 33
    //   27: bipush 255
    //   29: istore_3
    //   30: goto -9 -> 21
    //   33: aconst_null
    //   34: astore 4
    //   36: new 119	java/io/FileOutputStream
    //   39: dup
    //   40: aload_2
    //   41: invokespecial 122	java/io/FileOutputStream:<init>	(Ljava/io/File;)V
    //   44: astore 5
    //   46: sipush 1024
    //   49: newarray byte
    //   51: astore 14
    //   53: aload_1
    //   54: aload 14
    //   56: invokevirtual 128	java/io/InputStream:read	([B)I
    //   59: istore 15
    //   61: iload 15
    //   63: bipush 255
    //   65: if_icmpne +26 -> 91
    //   68: aload_1
    //   69: ifnull +7 -> 76
    //   72: aload_1
    //   73: invokevirtual 131	java/io/InputStream:close	()V
    //   76: aload 5
    //   78: ifnull +8 -> 86
    //   81: aload 5
    //   83: invokevirtual 132	java/io/FileOutputStream:close	()V
    //   86: iconst_0
    //   87: istore_3
    //   88: goto -67 -> 21
    //   91: aload 5
    //   93: aload 14
    //   95: iconst_0
    //   96: iload 15
    //   98: invokevirtual 135	java/io/FileOutputStream:write	([BII)V
    //   101: goto -48 -> 53
    //   104: astore 9
    //   106: aload 9
    //   108: astore 10
    //   110: aload 5
    //   112: astore 4
    //   114: ldc 72
    //   116: aload 10
    //   118: invokestatic 140	com/iknow/util/StringUtil:objToString	(Ljava/lang/Object;)Ljava/lang/String;
    //   121: invokestatic 146	com/iknow/util/Loger:e	(Ljava/lang/String;Ljava/lang/String;)V
    //   124: aload_2
    //   125: ifnull +8 -> 133
    //   128: aload_2
    //   129: invokevirtual 149	java/io/File:delete	()Z
    //   132: pop
    //   133: aload_1
    //   134: ifnull +7 -> 141
    //   137: aload_1
    //   138: invokevirtual 131	java/io/InputStream:close	()V
    //   141: aload 4
    //   143: ifnull +8 -> 151
    //   146: aload 4
    //   148: invokevirtual 132	java/io/FileOutputStream:close	()V
    //   151: bipush 255
    //   153: istore_3
    //   154: goto -133 -> 21
    //   157: astore 12
    //   159: ldc 72
    //   161: aload 12
    //   163: invokestatic 140	com/iknow/util/StringUtil:objToString	(Ljava/lang/Object;)Ljava/lang/String;
    //   166: invokestatic 146	com/iknow/util/Loger:e	(Ljava/lang/String;Ljava/lang/String;)V
    //   169: goto -28 -> 141
    //   172: astore 6
    //   174: aload_1
    //   175: ifnull +7 -> 182
    //   178: aload_1
    //   179: invokevirtual 131	java/io/InputStream:close	()V
    //   182: aload 4
    //   184: ifnull +8 -> 192
    //   187: aload 4
    //   189: invokevirtual 132	java/io/FileOutputStream:close	()V
    //   192: aload 6
    //   194: athrow
    //   195: astore 8
    //   197: ldc 72
    //   199: aload 8
    //   201: invokestatic 140	com/iknow/util/StringUtil:objToString	(Ljava/lang/Object;)Ljava/lang/String;
    //   204: invokestatic 146	com/iknow/util/Loger:e	(Ljava/lang/String;Ljava/lang/String;)V
    //   207: goto -25 -> 182
    //   210: astore 17
    //   212: ldc 72
    //   214: aload 17
    //   216: invokestatic 140	com/iknow/util/StringUtil:objToString	(Ljava/lang/Object;)Ljava/lang/String;
    //   219: invokestatic 146	com/iknow/util/Loger:e	(Ljava/lang/String;Ljava/lang/String;)V
    //   222: goto -146 -> 76
    //   225: astore 11
    //   227: goto -76 -> 151
    //   230: astore 7
    //   232: goto -40 -> 192
    //   235: astore 16
    //   237: goto -151 -> 86
    //   240: astore 6
    //   242: aload 5
    //   244: astore 4
    //   246: goto -72 -> 174
    //   249: astore 18
    //   251: aload 18
    //   253: astore 10
    //   255: goto -141 -> 114
    //
    // Exception table:
    //   from	to	target	type
    //   46	61	104	java/io/IOException
    //   91	101	104	java/io/IOException
    //   137	141	157	java/io/IOException
    //   36	46	172	finally
    //   114	133	172	finally
    //   178	182	195	java/io/IOException
    //   72	76	210	java/io/IOException
    //   146	151	225	java/io/IOException
    //   187	192	230	java/io/IOException
    //   81	86	235	java/io/IOException
    //   46	61	240	finally
    //   91	101	240	finally
    //   36	46	249	java/io/IOException
  }

  public boolean bCacheFileExist(String paramString)
  {
    int i;
    File localFile = new File(paramString);
    if ((localFile.exists()) && (localFile.canRead()))
      i = 1;
    while (true)
    {
      return i;
      i = 0;
    }
  }

  public void clearCache()
  {
    IKFileUtil.deleteAll(new File(getCachePath()));
  }

  public File createApkFile()
  {
    File localFile2;
    File localFile1 = new File(getApkPath() + ".apk");
    if (!(localFile1.exists()));
    try
    {
      localFile1.createNewFile();
      localFile2 = localFile1;
      return localFile2;
    }
    catch (IOException localIOException)
    {
      localIOException.printStackTrace();
      localFile2 = null;
    }
  }

  public String createAudioEmptyFile()
  {
    String str2;
    UUID localUUID = UUID.randomUUID();
    String str1 = getAudioPath() + localUUID.toString() + ".3gp";
    File localFile = new File(str1);
    if (!(localFile.exists()));
    try
    {
      localFile.createNewFile();
      str2 = str1;
      return str2;
    }
    catch (IOException localIOException)
    {
      localIOException.printStackTrace();
      str2 = null;
    }
  }

  public String createFileNameByUrl(String paramString)
  {
    return getCachePath() + getMd5(resetString(paramString));
  }

  public File createImageFile()
  {
    File localFile2;
    UUID localUUID = UUID.randomUUID();
    File localFile1 = new File(getImagePath() + localUUID.toString() + ".jpg");
    if (!(localFile1.exists()));
    try
    {
      localFile1.createNewFile();
      localFile2 = localFile1;
      return localFile2;
    }
    catch (IOException localIOException)
    {
      localIOException.printStackTrace();
      localFile2 = null;
    }
  }

  public String createImagefileName()
  {
    UUID localUUID = UUID.randomUUID();
    return getImagePath() + localUUID.toString() + ".jpg";
  }

  public void deleteFile(String paramString)
  {
    File localFile = new File(paramString);
    if (localFile.exists())
      localFile.delete();
  }

  public String getApkPath()
  {
    File localFile = new File(SystemUtil.fillSDPath(apkPath));
    if (!(localFile.exists()))
      localFile.mkdir();
    return SystemUtil.fillSDPath(apkPath);
  }

  public String getAudioPath()
  {
    File localFile = new File(SystemUtil.fillSDPath(audioPath));
    if (!(localFile.exists()))
      localFile.mkdir();
    return SystemUtil.fillSDPath(audioPath);
  }

  public String getCachePath()
  {
    return SystemUtil.fillSDPath(cachePath);
  }

  public File getFile(String paramString)
  {
    File localFile;
    if (paramString.contains(".apk"))
    {
      int i = paramString.lastIndexOf(47);
      int j = paramString.length();
      String str2 = paramString.substring(i + 1, j);
      localFile = new File(getApkPath() + str2);
    }
    while (true)
    {
      return localFile;
      String str1 = getMd5(resetString(paramString));
      localFile = new File(getCachePath() + str1 + ".iks");
    }
  }

  public String getFileName(String paramString)
  {
    String str = getMd5(resetString(paramString));
    return getCachePath() + str + ".iks";
  }

  public String getFilePath(String paramString)
  {
    String str;
    if (StringUtil.isEmpty(paramString))
      str = null;
    while (true)
    {
      while (true)
      {
        return str;
        File localFile = getFile(paramString);
        if ((localFile == null) || (!(localFile.exists())) || (!(localFile.canRead())))
          break;
        str = localFile.getAbsolutePath();
      }
      str = null;
    }
  }

  public String getImageFileName(String paramString)
  {
    String str = getMd5(resetString(paramString)) + ".jpg";
    return getImagePath() + str;
  }

  public String getImagePath()
  {
    File localFile = new File(SystemUtil.fillSDPath(imagePath));
    if (!(localFile.exists()))
      localFile.mkdir();
    return SystemUtil.fillSDPath(imagePath);
  }

  public MessageDigest getMessageDigest()
  {
    MessageDigest localMessageDigest;
    if (this.mDigest != null)
    {
      localMessageDigest = this.mDigest;
      return localMessageDigest;
    }
    try
    {
      this.mDigest = MessageDigest.getInstance("MD5");
      localMessageDigest = this.mDigest;
    }
    catch (NoSuchAlgorithmException localNoSuchAlgorithmException)
    {
      throw new RuntimeException("No MD5 algorithm.");
    }
  }

  public String getThumbmailFileName(String paramString)
  {
    String str1;
    String str2;
    String str3;
    int i = paramString.lastIndexOf(File.separator);
    if (-1 != i)
    {
      str2 = paramString.substring(i + 1, paramString.length());
      if (-1 == str2.lastIndexOf("."))
      {
        str3 = str2 + ".jpg";
        str1 = getThumbmailPath() + str3;
      }
    }
    while (true)
    {
      while (true)
      {
        return str1;
        str3 = str2.replace(".jpeg", ".jpg");
      }
      str1 = null;
    }
  }

  public String getThumbmailPath()
  {
    File localFile = new File(SystemUtil.fillSDPath(thumbmailPath));
    if (!(localFile.getParentFile().exists()))
      localFile.getParentFile().mkdir();
    if (!(localFile.exists()))
      localFile.mkdir();
    return SystemUtil.fillSDPath(thumbmailPath);
  }

  public String readFile(String paramString)
  {
    File localFile;
    String str1;
    if (StringUtil.isEmpty(paramString))
      str1 = null;
    while (true)
    {
      return str1;
      localFile = getFile(paramString);
      if (localFile.exists())
        break;
      str1 = null;
    }
    try
    {
      StringBuilder localStringBuilder;
      String str2;
      while (true)
      {
        BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(localFile), "UTF-8"));
        localStringBuilder = new StringBuilder();
        str2 = localBufferedReader.readLine();
        if (str2 != null)
          break;
        str1 = localStringBuilder.toString();
      }
      localStringBuilder.append(str2);
    }
    catch (Exception localException)
    {
      while (true)
      {
        Loger.e("CacheSystem", localException.getMessage());
        localException.printStackTrace();
        str1 = null;
      }
    }
  }

  public String resetString(String paramString)
  {
    String str3;
    if (StringUtil.isEmpty(paramString))
      str3 = null;
    while (true)
    {
      while (true)
      {
        return str3;
        if (paramString.contains("jsessionid"))
          break;
        str3 = paramString;
      }
      int i = paramString.indexOf("jsessionid");
      int j = paramString.indexOf("?");
      String str1 = paramString.substring(0, i);
      String str2 = paramString.substring(j + 1, paramString.length());
      str3 = str1 + str2;
    }
  }

  public int saveFile(String paramString, InputStream paramInputStream)
  {
    File localFile1;
    int i;
    if (StringUtil.isEmpty(paramString))
      i = -1;
    while (true)
    {
      return i;
      if (paramString.contains(".apk"))
      {
        int j = paramString.lastIndexOf(47);
        int k = paramString.length();
        paramString = paramString.substring(j + 1, k);
      }
      localFile1 = getFile(paramString);
      if (localFile1 != null)
        break;
      i = -1;
    }
    File localFile2 = localFile1.getParentFile();
    if (!(localFile2.exists()))
      localFile2.mkdirs();
    if (localFile1.exists())
      localFile1.delete();
    try
    {
      localFile1.createNewFile();
      i = write(paramInputStream, localFile1);
    }
    catch (IOException localIOException)
    {
      Loger.e("com.iknow.file.CacheSystem", StringUtil.objToString(localIOException));
      i = -1;
    }
  }

  public int saveFile(String paramString1, String paramString2)
  {
    File localFile1;
    int i;
    if (StringUtil.isEmpty(paramString1))
      i = -1;
    while (true)
    {
      return i;
      localFile1 = getFile(paramString1);
      if (localFile1 != null)
        break;
      i = -1;
    }
    File localFile2 = localFile1.getParentFile();
    if (!(localFile2.exists()))
      localFile2.mkdirs();
    if (localFile1.exists())
      localFile1.delete();
    try
    {
      localFile1.createNewFile();
    }
    catch (IOException localIOException)
    {
      try
      {
        while (true)
        {
          FileOutputStream localFileOutputStream = new FileOutputStream(localFile1);
          OutputStreamWriter localOutputStreamWriter = new OutputStreamWriter(localFileOutputStream, "utf-8");
          localOutputStreamWriter.write(paramString2);
          localOutputStreamWriter.close();
          localFileOutputStream.close();
          i = 0;
        }
        localIOException = localIOException;
        Loger.e("com.iknow.file.CacheSystem", StringUtil.objToString(localIOException));
        i = -1;
      }
      catch (Exception localException)
      {
        Loger.e("CacheSystem", localException.getMessage());
        localException.printStackTrace();
        i = -1;
      }
    }
  }
}